package com.neusoft.yuejian26.yuejian_26.Activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.yuejian26.yuejian_26.Bean.Result;
import com.neusoft.yuejian26.yuejian_26.Other.CacheInfo;
import com.neusoft.yuejian26.yuejian_26.R;
import com.neusoft.yuejian26.yuejian_26.model.UserModel;
import com.neusoft.yuejian26.yuejian_26.network.RetrofitCallback;

import retrofit2.Call;

public class LoginActivity extends AppCompatActivity {
    public EditText phone, password;
    public Button lg_btn;
    public TextView lg_to_reg;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        intview();
        intEvent();
    }

    private class logining implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            String utel = phone.getText().toString().trim();
            String pwd = password.getText().toString().trim();
            if (!utel.equals("") && !pwd.equals("")) {
                UserModel userModel = new UserModel();
                Call call = userModel.userLogin(utel, pwd);
                userModel.bindCallback(call, new RetrofitCallback() {
                    @Override
                    public void onResponse(Object o) {
                        Result result = (Result) o;
                        int uid = result.getCode();
                        if (uid != 0) {
                            //登录成功，保存用户id
                            CacheInfo cacheInfo = new CacheInfo(LoginActivity.this);
                            cacheInfo.setUserIdCache(uid + "");
                            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent();
                            intent.setClass(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                        } else {
                            Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            } else {
                Toast.makeText(LoginActivity.this, "请输入相应选项", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void intview() {
        phone = (EditText) findViewById(R.id.input_tel);
        password = (EditText) findViewById(R.id.input_pwd);
        lg_btn = (Button) findViewById(R.id.lg_btn);
        lg_to_reg = (TextView) findViewById(R.id.to_reg);
    }

    private void intEvent(){
        lg_btn.setOnClickListener(new logining());
        lg_to_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this, RegActivity.class);
                startActivity(intent);
            }
        });
    }

}
